package com.thoughtworks.retailstore.adapters;

import java.util.ArrayList;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.thoughtworks.models.CartModel;
import com.thoughtworks.retailstore.R;
import com.thoughtworks.retailstore.fragments.ProductDetailsFragment;
import com.thoughtworks.retailstore.utils.Constants;

// TODO: Auto-generated Javadoc
/**
 * The Class CartListAdapter.
 */
public class CartListAdapter extends BaseAdapter {

	/** The context. */
	private Context context ;

	/** The inflater. */
	private LayoutInflater inflater ;

	/** The cart list. */
	private ArrayList<CartModel> cartList ;

	/** The total price tv. */
	private TextView totalPriceTv ;

	/** The cart. */
	CartModel cart ;

	/**
	 * Instantiates a new cart list adapter.
	 *
	 * @param c the c
	 * @param _cart the _cart
	 * @param _totalPriceTextView the _total price text view
	 */
	public  CartListAdapter(Context c, CartModel _cart,
			TextView _totalPriceTextView )
	{
		context = c ;
		inflater = LayoutInflater.from(context);
		cart = _cart ;
		totalPriceTv = _totalPriceTextView ;
	}


	@Override
	public int getCount() {

		return cart.getProducts().size();
	}


	@Override
	public Object getItem(int position) {

		return position;
	}

	@Override
	public long getItemId(int itemId) {

		return itemId;
	}


	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		ViewHolder h ;

		if(convertView == null)
		{
			h = new ViewHolder() ;
			convertView = inflater.inflate(R.layout.cart_items_layout, null);
			h.productName = (TextView)convertView.findViewById(R.id.cartProdNameTv);
			h.productPrice = (TextView)convertView.findViewById(R.id.cartProdPriceTv);
			h.removeButton = (ImageButton)convertView.findViewById(R.id.removeBtn);
			convertView.setTag(h);
		}
		else
		{
			h = (ViewHolder)convertView.getTag();
		}

		String prodName = ""; 
		int prodPrice = 0 ;

		prodName = cart.getProducts().get(position).getProductName();
		prodPrice = cart.getProducts().get(position).getProductPrice();
		h.productName.setText(prodName);
		h.productPrice.setText(Integer.toString(prodPrice)+" INR");

		h.removeButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				try{
					cart.removeFromCart(cart.getProducts().get(position));
					notifyDataSetChanged();
				}
				catch(Exception e){
					e.printStackTrace();
				}
			}
		});
		convertView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				_launchProductDetailsFrag(position);
			}
		});
		return convertView;
	}


	@Override
	public void notifyDataSetChanged() {

		super.notifyDataSetChanged();
		totalPriceTv.setText(Integer.toString(cart.cartTotal())+ " INR");
	}

	/**
	 * Lauch product details frag.
	 *
	 * @param position the position
	 */
	public void _launchProductDetailsFrag(int position)
	{
		int grpPos = cart.getProducts().get(position).getCategId();
		int childPos = cart.getProducts().get(position).getProdId();
		ProductDetailsFragment frag = ProductDetailsFragment.
				newInstance(grpPos, childPos);
		FragmentTransaction ft = Constants.fragMgr.beginTransaction();
		ft.replace(R.id.container,frag,"product_details_fragment");
		ft.addToBackStack("product_details_fragment");
		ft.commit();
	}
	/**
	 * The Class ViewHolder.
	 */
	class ViewHolder
	{

		/** The product name. */
		TextView productName ;

		/** The product price. */
		TextView productPrice ;

		/** The remove button. */
		ImageButton removeButton ;
	}
}
